package c.c.b.a.b.a.h;

import c.c.b.a.a.f;
import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* compiled from: Http2Connection */
public final class g implements Closeable {
    public static final ExecutorService s;
    public final boolean a;
    public final d b;
    public final Map<Integer, q> c = new LinkedHashMap();
    public final String d;
    public int e;
    public int f;
    public boolean g;
    public final ExecutorService h;
    public final u i;
    public long j = 0;
    public long k;
    public v l = new v();
    public final v m;
    public boolean n;
    public final Socket o;
    public final r p;
    public final e q;
    public final Set<Integer> r;

    /* compiled from: Http2Connection */
    public static class c {
        public Socket a;
        public String b;
        public c.c.b.a.a.g c;
        public f d;
        public d e = d.a;

        public c(boolean z) {
        }
    }

    /* compiled from: Http2Connection */
    public static abstract class d {
        public static final d a = new a();

        /* compiled from: Http2Connection */
        public static class a extends d {
            public void b(q qVar) throws IOException {
                qVar.a(b.REFUSED_STREAM);
            }
        }

        public void a(g gVar) {
        }

        public abstract void b(q qVar) throws IOException;
    }

    /* compiled from: Http2Connection */
    public class a extends c.c.b.a.b.a.d {
        public final /* synthetic */ int b;
        public final /* synthetic */ b c;

        public a(String str, Object[] objArr, int i, b bVar) {
            this.b = i;
            this.c = bVar;
            super(str, objArr);
        }

        public void a() {
            try {
                g gVar = g.this;
                gVar.p.n(this.b, this.c);
            } catch (IOException unused) {
            }
        }
    }

    /* compiled from: Http2Connection */
    public class b extends c.c.b.a.b.a.d {
        public final /* synthetic */ int b;
        public final /* synthetic */ long c;

        public b(String str, Object[] objArr, int i, long j) {
            this.b = i;
            this.c = j;
            super(str, objArr);
        }

        public void a() {
            try {
                g.this.p.k(this.b, this.c);
            } catch (IOException unused) {
            }
        }
    }

    /* compiled from: Http2Connection */
    public class e extends c.c.b.a.b.a.d implements c.c.b.a.b.a.h.p.b {
        public final p b;

        public e(p pVar) {
            super("OkHttp %s", r3.d);
            this.b = pVar;
        }

        /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x001f */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing block: B:16:?, code skipped:
            r0 = c.c.b.a.b.a.h.b.c;
     */
        /* JADX WARNING: Missing block: B:18:?, code skipped:
            r4.c.u(r0, r0);
     */
        /* JADX WARNING: Missing block: B:22:0x002c, code skipped:
            r2 = th;
     */
        public void a() {
            /*
            r4 = this;
            r0 = c.c.b.a.b.a.h.b.INTERNAL_ERROR;
            r1 = r4.b;	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            r1.o(r4);	 Catch:{ IOException -> 0x001e, all -> 0x001b }
        L_0x0007:
            r1 = r4.b;	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            r2 = 0;
            r1 = r1.q(r2, r4);	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            if (r1 == 0) goto L_0x0011;
        L_0x0010:
            goto L_0x0007;
        L_0x0011:
            r1 = c.c.b.a.b.a.h.b.NO_ERROR;	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            r0 = c.c.b.a.b.a.h.b.CANCEL;	 Catch:{ IOException -> 0x001f }
            r2 = c.c.b.a.b.a.h.g.this;	 Catch:{ IOException -> 0x0026 }
            r2.u(r1, r0);	 Catch:{ IOException -> 0x0026 }
            goto L_0x0026;
        L_0x001b:
            r2 = move-exception;
            r1 = r0;
            goto L_0x002d;
        L_0x001e:
            r1 = r0;
        L_0x001f:
            r0 = c.c.b.a.b.a.h.b.PROTOCOL_ERROR;	 Catch:{ all -> 0x002c }
            r1 = c.c.b.a.b.a.h.g.this;	 Catch:{ IOException -> 0x0026 }
            r1.u(r0, r0);	 Catch:{ IOException -> 0x0026 }
        L_0x0026:
            r0 = r4.b;
            c.c.b.a.b.a.e.n(r0);
            return;
        L_0x002c:
            r2 = move-exception;
        L_0x002d:
            r3 = c.c.b.a.b.a.h.g.this;	 Catch:{ IOException -> 0x0032 }
            r3.u(r1, r0);	 Catch:{ IOException -> 0x0032 }
        L_0x0032:
            r0 = r4.b;
            c.c.b.a.b.a.e.n(r0);
            throw r2;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.c.b.a.b.a.h.g$e.a():void");
        }
    }

    static {
        Class cls = g.class;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        SynchronousQueue synchronousQueue = new SynchronousQueue();
        byte[] bArr = c.c.b.a.b.a.e.a;
        s = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, timeUnit, synchronousQueue, new c.c.b.a.b.a.f("OkHttp Http2Connection", true));
    }

    public g(c cVar) {
        v vVar = new v();
        this.m = vVar;
        this.n = false;
        this.r = new LinkedHashSet();
        this.i = u.a;
        this.a = true;
        this.b = cVar.e;
        this.f = 1;
        this.f = 3;
        this.l.a(7, 16777216);
        this.d = cVar.b;
        this.h = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new c.c.b.a.b.a.f(c.c.b.a.b.a.e.i("OkHttp %s Push Observer", r4), true));
        vVar.a(7, 65535);
        vVar.a(5, 16384);
        this.k = (long) vVar.b();
        this.o = cVar.a;
        this.p = new r(cVar.d, true);
        this.q = new e(new p(cVar.c, true));
    }

    public void close() throws IOException {
        u(b.NO_ERROR, b.CANCEL);
    }

    public boolean h0(int i) {
        return i != 0 && (i & 1) == 0;
    }

    public synchronized int n() {
        int i;
        v vVar = this.m;
        i = Integer.MAX_VALUE;
        if ((vVar.a & 16) != 0) {
            i = vVar.b[4];
        }
        return i;
    }

    public synchronized q o(int i) {
        return (q) this.c.get(Integer.valueOf(i));
    }

    public void q(int i, long j) {
        s.execute(new b("OkHttp Window Update %s stream %d", new Object[]{this.d, Integer.valueOf(i)}, i, j));
    }

    public void r(int i, b bVar) {
        s.execute(new a("OkHttp %s stream %d", new Object[]{this.d, Integer.valueOf(i)}, i, bVar));
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:26:0x0056 */
    /* JADX WARNING: Can't wrap try/catch for region: R(3:26|27|28) */
    /* JADX WARNING: Missing block: B:16:?, code skipped:
            r3 = java.lang.Math.min((int) java.lang.Math.min(r12, r3), r8.p.d);
            r6 = (long) r3;
            r8.k -= r6;
     */
    /* JADX WARNING: Missing block: B:28:0x005b, code skipped:
            throw new java.io.InterruptedIOException();
     */
    public void s(int r9, boolean r10, c.c.b.a.a.e r11, long r12) throws java.io.IOException {
        /*
        r8 = this;
        r0 = 0;
        r1 = 0;
        r3 = (r12 > r1 ? 1 : (r12 == r1 ? 0 : -1));
        if (r3 != 0) goto L_0x000d;
    L_0x0007:
        r12 = r8.p;
        r12.s(r10, r9, r11, r0);
        return;
    L_0x000d:
        r3 = (r12 > r1 ? 1 : (r12 == r1 ? 0 : -1));
        if (r3 <= 0) goto L_0x005e;
    L_0x0011:
        monitor-enter(r8);
    L_0x0012:
        r3 = r8.k;	 Catch:{ InterruptedException -> 0x0056 }
        r5 = (r3 > r1 ? 1 : (r3 == r1 ? 0 : -1));
        if (r5 > 0) goto L_0x0030;
    L_0x0018:
        r3 = r8.c;	 Catch:{ InterruptedException -> 0x0056 }
        r4 = java.lang.Integer.valueOf(r9);	 Catch:{ InterruptedException -> 0x0056 }
        r3 = r3.containsKey(r4);	 Catch:{ InterruptedException -> 0x0056 }
        if (r3 == 0) goto L_0x0028;
    L_0x0024:
        r8.wait();	 Catch:{ InterruptedException -> 0x0056 }
        goto L_0x0012;
    L_0x0028:
        r9 = new java.io.IOException;	 Catch:{ InterruptedException -> 0x0056 }
        r10 = "stream closed";
        r9.<init>(r10);	 Catch:{ InterruptedException -> 0x0056 }
        throw r9;	 Catch:{ InterruptedException -> 0x0056 }
    L_0x0030:
        r3 = java.lang.Math.min(r12, r3);	 Catch:{ all -> 0x0054 }
        r4 = (int) r3;	 Catch:{ all -> 0x0054 }
        r3 = r8.p;	 Catch:{ all -> 0x0054 }
        r3 = r3.d;	 Catch:{ all -> 0x0054 }
        r3 = java.lang.Math.min(r4, r3);	 Catch:{ all -> 0x0054 }
        r4 = r8.k;	 Catch:{ all -> 0x0054 }
        r6 = (long) r3;	 Catch:{ all -> 0x0054 }
        r4 = r4 - r6;
        r8.k = r4;	 Catch:{ all -> 0x0054 }
        monitor-exit(r8);	 Catch:{ all -> 0x0054 }
        r12 = r12 - r6;
        r4 = r8.p;
        if (r10 == 0) goto L_0x004f;
    L_0x0049:
        r5 = (r12 > r1 ? 1 : (r12 == r1 ? 0 : -1));
        if (r5 != 0) goto L_0x004f;
    L_0x004d:
        r5 = 1;
        goto L_0x0050;
    L_0x004f:
        r5 = 0;
    L_0x0050:
        r4.s(r5, r9, r11, r3);
        goto L_0x000d;
    L_0x0054:
        r9 = move-exception;
        goto L_0x005c;
    L_0x0056:
        r9 = new java.io.InterruptedIOException;	 Catch:{ all -> 0x0054 }
        r9.<init>();	 Catch:{ all -> 0x0054 }
        throw r9;	 Catch:{ all -> 0x0054 }
    L_0x005c:
        monitor-exit(r8);	 Catch:{ all -> 0x0054 }
        throw r9;
    L_0x005e:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.b.a.b.a.h.g.s(int, boolean, c.c.b.a.a.e, long):void");
    }

    public void t(b bVar) throws IOException {
        synchronized (this.p) {
            synchronized (this) {
                if (this.g) {
                    return;
                }
                this.g = true;
                int i = this.e;
                this.p.o(i, bVar, c.c.b.a.b.a.e.a);
            }
        }
    }

    public void u(b bVar, b bVar2) throws IOException {
        IOException e;
        q[] qVarArr = null;
        try {
            t(bVar);
            e = null;
        } catch (IOException e2) {
            e = e2;
        }
        synchronized (this) {
            if (!this.c.isEmpty()) {
                qVarArr = (q[]) this.c.values().toArray(new q[this.c.size()]);
                this.c.clear();
            }
        }
        if (qVarArr != null) {
            for (q a : qVarArr) {
                try {
                    a.a(bVar2);
                } catch (IOException e3) {
                    if (e != null) {
                        e = e3;
                    }
                }
            }
        }
        try {
            this.p.close();
        } catch (IOException e4) {
            if (e == null) {
                e = e4;
            }
        }
        try {
            this.o.close();
        } catch (IOException e5) {
            e = e5;
        }
        if (e != null) {
            throw e;
        }
    }

    public synchronized q v(int i) {
        q qVar;
        qVar = (q) this.c.remove(Integer.valueOf(i));
        notifyAll();
        return qVar;
    }

    public void w() throws IOException {
        this.p.v();
    }
}
